package com.llkj.contractmanagement.contractmanagement.contract;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.llkj.contractmanagement.main.BaseFragment;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.customview.TitleBar;

import butterknife.Bind;

/**
 * author: dong.yj
 * created on: 2016/8/15 9:16
 * description:合同界面
 */
public class ContractFragment extends BaseFragment implements View.OnClickListener {


    @Bind(R.id.rl_contract_movie)
    RelativeLayout rlMovie;
    @Bind(R.id.rl_contract_broker)
    RelativeLayout rlBroker;
    @Bind(R.id.rl_contract_model)
    RelativeLayout rlModel;
    @Bind(R.id.iv_movie)
    ImageView ivMovie;
    @Bind(R.id.iv_jingjiren)
    ImageView ivBroker;
    @Bind(R.id.iv_contract_model)
    ImageView ivModel;
    @Bind(R.id.title_contract)
    TitleBar titleBar;
    @Bind(R.id.rl_search)
    RelativeLayout rl_search;

    private MovieFragment movieFragment;
    private BrokerFragment brokerFragment;
    private ContractModelFragment contractModelFragment;

    @Override
    public int getContentViewLayout() {
        return R.layout.fragment_contract;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    private void init() {
        setListener();
        setSelection(0);
    }

    private void setListener() {
        titleBar.setTopBarClickListener(this);
        rlMovie.setOnClickListener(this);
        rlBroker.setOnClickListener(this);
        rlModel.setOnClickListener(this);
        rl_search.setOnClickListener(this);
    }

    private void setSelected(View view) {
        rlMovie.setSelected(false);
        rlBroker.setSelected(false);
        rlModel.setSelected(false);
        view.setSelected(true);
        if (view.getId() == R.id.rl_contract_movie) {
            ivMovie.setVisibility(view.VISIBLE);
            ivBroker.setVisibility(view.INVISIBLE);
            ivModel.setVisibility(view.INVISIBLE);
        } else if (view.getId() == R.id.rl_contract_broker) {
            ivMovie.setVisibility(view.INVISIBLE);
            ivBroker.setVisibility(view.VISIBLE);
            ivModel.setVisibility(view.INVISIBLE);
        } else if (view.getId() == R.id.rl_contract_model) {
            ivBroker.setVisibility(view.INVISIBLE);
            ivMovie.setVisibility(view.INVISIBLE);
            ivModel.setVisibility(view.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_contract_movie://影视
                setSelection(0);
                break;
            case R.id.rl_contract_broker://经济
                setSelection(1);
                break;
            case R.id.rl_contract_model://模板合同
                setSelection(2);
                break;
            case R.id.rl_search:
                Intent intent = new Intent(ctx, SearchResultActivity.class);
                //全局搜索，type传0
                intent.putExtra("type", 0);
                startActivity(intent);
                break;
        }
    }

    private void setSelection(int index) {
        FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);
        switch (index) {
            case 0:// 影视
                setSelected(rlMovie);
                if (movieFragment == null) {
                    movieFragment = new MovieFragment();
                    fragmentTransaction.add(R.id.fl_contract_content, movieFragment);
                } else {
                    fragmentTransaction.show(movieFragment);
                }
                fragmentTransaction.commit();
                break;
            case 1:// 经济
                setSelected(rlBroker);
                if (brokerFragment == null) {
                    brokerFragment = new BrokerFragment();
                    fragmentTransaction.add(R.id.fl_contract_content, brokerFragment);
                } else {
                    fragmentTransaction.show(brokerFragment);
                }
                fragmentTransaction.commit();
                break;
            case 2:// 模板合同
                setSelected(rlModel);
                if (contractModelFragment == null) {
                    contractModelFragment = new ContractModelFragment();
                    fragmentTransaction.add(R.id.fl_contract_content, contractModelFragment);
                } else {
                    fragmentTransaction.show(contractModelFragment);
                }
                fragmentTransaction.commitAllowingStateLoss();
                break;
        }

    }

    /**
     * 隐藏所有的fragment
     *
     * @param fragmentTransaction
     */
    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (movieFragment != null) {
            fragmentTransaction.hide(movieFragment);
        }
        if (brokerFragment != null) {
            fragmentTransaction.hide(brokerFragment);
        }
        if (contractModelFragment != null) {
            fragmentTransaction.hide(contractModelFragment);
        }
    }

    @Override
    public void rightClick() {
        super.rightClick();
        Intent intent = new Intent(ctx, SystemMessageActivity.class);
        startActivity(intent);
    }
}
